<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/reset.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/transaction/allocation_index.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/patrol/project_device_alert.css">
</script>
<div class="layui-fluid">
    <div class="layui-table-title">
        工单详细信息
    </div>
    <div class="layui-table-box-1">
        <ul class="layui-row">
            <li class="layui-col-md4 layui-col-sm4">
                <div>工单名称：<span id='tcReqName'></span></div>
                <div>OA申请编号：<span id='fil02'></span></div>
                <div>申请日期：<span id="fil10"></span></div>
            </li>
            <li class="layui-col-md4 layui-col-sm4">
                <div>工单编号：<span id='fil01'></span></div>
                <div>
                    设备所属公司：<span id="oldazp"></span>
                </div>
            </li>
            <li class="layui-col-md4 layui-col-sm4">
                <div>工单状态：<span id="fil05"></span></div>
                <div>申请人：<span id="newazp"></span></div>
            </li>
        </ul>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">信息变更工单说明:</label>
            <div class="layui-input-block">
                <textarea name="desc" id="fil21" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="desc" id="fil07" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="footer layui-btn-containner" style="height: 50px;">
            <button class="hold layui-btn layui-btn-radius layui-btn-primary" lay-submit
                lay-filter="LAY-submit-bc">保存</button>
        </div>
    </div>
    <div class="title-btn layui-btn-containner">
        <button class="add layui-btn layui-btn-radius layui-btn-primary">
            <i class="add-icon layui-icon layui-icon-add-circle" style="font-size: 15px;"></i>
            <span class="btn-inner">添加设备</span>
        </button>
        <!-- 批量修改 -->
        <button class="modify layui-btn layui-btn-radius layui-btn-primary editBatch">
            <span class="btn-inner">批量修改</span>
        </button>
        <button class="modify layui-btn layui-btn-radius layui-btn-primary delBatch" style="background-color: #FF4C4C;">
            <span class="btn-inner">批量删除</span>
        </button>
    </div>
    <table class="layui-table" id="LAY-app-content-comm" lay-filter="LAY-app-content-comm"></table>
    <div class="footer layui-btn-containner">
        <button class="commit layui-btn layui-btn-radius layui-btn-primary" lay-submit
            lay-filter="LAY-submit-tj">提交</button>

        <button class="cancel layui-btn layui-btn-radius layui-btn-primary" lay-submit
            lay-filter="LAY-submit-qx">返回</button>
    </div>

</div>




<script type="text/html" id="buttonTpl1">
    <span class="table-edit" lay-event="edit"></span>
    <span class="table-delete" lay-event="del"></span>
</script>

<script type="text/html" id="buttonTpl">
    <span>{{d.tcFtyName?d.tcFtyName:''}}{{d.tcFlrName?d.tcFlrName:''}}{{d.tcProcName?d.tcProcName:''}}{{d.tcLineName?d.tcLineName:''}}</span>
</script>

<script type="text/html" id="buttonTpl2">
    <span>{{d.tcFtyName1}}{{d.tcFlrName1}}{{d.tcProcName1}}{{d.tcLineName1}}</span>
</script>


<!-- 巡检设备选择 -->
<script type="text/html" id="addDeviceTpl">
    <div class="leon-style-alert">
        <div class="layui-alert-box">
            <div class="layui-alert-title">
                <span class="layui-title-inner">信息变更添加设备选择</span>
                <span class="layui-title-screen">
                    <div class="screen-btn">
                        <form class="layui-form" action="">
                            <input type="search" name="keyWord" class="search-blank-inner searchInput"
                                placeholder="搜索" autocomplete="off"/>
                            <button type="button" lay-submit lay-filter="queryTableDeviceHeader"
                                class="search-blank-icon"></button>
                        </form>
                    </div>
                </span>
            </div>
            <div class="layui-content">
                <div class="con1 toggle-btn">
                    <span class="screen">
                        <i class="query-icon  layui-icon"></i>
                        <span class="query-inner">查询条件</span>
                    </span>
                    <i class="icon layui-icon layui-icon-down"></i>
                </div>
                <div class="con2 toggle-content">
                    <form action="" class="layui-form">
                        <label for="">设备编号:</label>
                        <input type="text" name="fia01" />
                        <label for="">设备名称:</label>
                        <input type="text" name="fia02" />
                        <label for="">供应商:</label>
                        <input type="text" name="fia09" />
                        <button type="button" lay-submit lay-filter="queryTableDevice"
                            class="query layui-btn layui-btn-radius layui-btn-primary">
                            <span class="search-icon"></span>
                            <span class="btn-inner">查询</span>
                        </button>
                    </form>
                </div>
                <table class="layui-table" id="table-device-alert"></table>
            </div>
            <div class="footer layui-btn-containner">
                <button class="btn1 layui-btn layui-btn-radius layui-btn-primary">添加</button>
                <button class="btn2 layui-btn layui-btn-radius layui-btn-primary">取消</button>
            </div>
        </div>
    </div>
</script>

<script>
    var devAlt = {
    }
    layui.use(['admin', 'form', 'user', 'api', 'table', 'laydate', 'utill', 'layer'], function () {
        var $ = layui.$,
            setter = layui.setter,
            admin = layui.admin,
            form = layui.form,
            router = layui.router(),
            api = layui.api,
            view = layui.view,
            laydate = layui.laydate,
            table = layui.table,
            utill = layui.utill,
            layer = layui.layer,
            search = router.search;
        var href = JSON.parse(sessionStorage.getItem('layui-this'));
        $(href).addClass('layui-this');
        rander()

        $('[data-jump="transaction/paymentAccount"]').addClass('layui-this').attr('color', '#FFF');
        form.on('submit(LAY-submit-bc)', function (data) {
            var field = data.field
            var data = {
                tcReqId: layui.router().search.id,
                fil07: $('#fil07').val(),
                fil21: $('#fil21').val(),
            }
            admin.req({
                url: api.host + api.updateByPrimaryKey,
                type: "post",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                done: function (res) {
                    layer.msg('保存成功');
                    $('#fil07').val(res.data.fil07)
                    $('#fil21').val(res.data.fil21)

                }
            });

        })

        form.on('submit(LAY-submit-tj)', function (data) {
            var field = data.field
            utill.loading.show();
            var data = {
                tcReqId: layui.router().search.id
            }
            admin.req({
                url: api.host + api.devsendBGOA,
                type: "post",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                success: function (res) {
                    if (res.code == 0) {
                        location.hash = '/transaction/paymentAccount'
                    }
                    utill.loading.hide();
                }
            });

        })

        form.on('submit(LAY-submit-qx)', function (data) {
            location.hash = '/transaction/paymentAccount'
        })

        function rander() {
            utill.columnCustom('#LAY-app-content-comm', "TcEamDevABody", {
                // 自己表格的接口
                url: api.host + api.selectDevAltBody,
                where: { fil01: layui.router().search.fil01 },
                id: 'LAY-app-content-comm',
                elem: '#LAY-app-content-comm',
                toolbar: {
                    title: '操作',
                    templet: '#buttonTpl1',
                },
            });

            table.on('tool(LAY-app-content-comm)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    // var data = {
                    //     tcDetailId: obj.data.tcDetailId,
                    // }
                    var arr=new Array();
                    arr.push(obj.data.tcDetailId);
                    layer.confirm('确定要删除该设备吗？', {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        admin.req({
                            url: api.host + api.devdelDevAlt,
                            data: JSON.stringify(arr),
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            done: function (res) {
                                layui.table.reload('#LAY-app-content-comm'); //重载表格
                                layer.closeAll()
                            }
                        });
                    }, function () {
                        layer.close();
                    });
                } else if (obj.event === 'edit') {
                    sessionStorage.setItem('company',$('#oldazp').data('id'));
                    sessionStorage.setItem('fia01', obj.data.tcDetailFia01)
                    devAlt = obj.data
                    admin.popup({
                        title: '编辑设备信息',
                        area: ['800px', '700px'],
                        id: 'LAY-popup-content-tags',
                        success: function (layero, index) {
                            view(this.id).render('transaction/branch-four/from1')
                                .done(function () {
                                    //监听提交
                                    form.on('submit(LAY-submit)', function (data) {
                                        var field = data.field; //获取提交的字段
                                        var data = {

                                        }
                                        data.tcDetailId = obj.data.tcDetailId

                                        data.tcDetailFia03 = obj.data.fia03
                                        data.tcDetailFia09 = obj.data.fia09
                                        data.tcDetailFia04 = obj.data.fia04
                                        data.tcDetailFia05 = obj.data.fia05
                                        data.tcDetailFia01 = field.fia01
                                        data.tcDetailFia02 = field.fia02
                                        data.tcDetailFia11 = field.tcDetailFia11.split("/")[1].replace(/\s+/g, "")
                                        data.tcDetailFia10 = field.tcDetailFia10.split("/")[0].replace(/\s+/g, "")
                                        data.tcDetailFia14 = field.tcDetailFia14
                                            .split("/")[0].replace(/\s+/g, "")
                                        data.tcDetailFia16 =
                                            field
                                                .tcDetailFia16.split("/")[0]
                                                .replace(/\s+/g,
                                                    "")
                                        data.tcDetailFia15 =
                                            field
                                                .tcDetailFia15.split("/")[0]
                                                .replace(/\s+/g,
                                                    "")
                                        data.tcDetailFia17 =
                                            field
                                                .tcDetailFia17.split("/")[0]
                                                .replace(/\s+/g,
                                                    "")
                                        data.tcDetailFia011 =
                                            field
                                                .tcDetailFia011.replace(/\s+/g, "")
                                        data.tcDetailFiauser =
                                            field
                                                .tcDetailFiauser.split("/")[0]
                                                .replace(
                                                    /\s+/g, "")

                                        var param = JSON.stringify(data)
                                        admin.req({
                                            url: api.host + api
                                                .devupdatetDevAlt,
                                            dataType: 'json',

                                            type: "POST",
                                            contentType: 'application/json',
                                            data: param,
                                            done: function (res) {
                                                layui.table.reload(
                                                    '#LAY-app-content-comm'
                                                ); //重载表格
                                                layer.closeAll()

                                            }
                                        });


                                    });
                                });
                        }
                    });
                }
            });
        }
        admin.req({
            url: api.host + api.devselectdetails,
            data: {
                tcDocId: layui.router().search.id
            },
            done: function (res) {
                var text = ''
                if (res.data.fil.fil05 == 0) {

                    text = '未提交'
                } else if (res.data.fil.fil05 == 1) {
                    text = '待审核'
                } else if (res.data.fil.fil05 == 6) {
                    text = 'OA送审中'
                }
                $('#fil05').html(text)
                $('#fil07').val(res.data.fil.fil07)
                $('#fil21').val(res.data.fil.fil21)
                $('#fil01').html(res.data.fil.fil01)
                $('#fil02').html(res.data.fil.fil02)
                $('#fil21').html(res.data.fil.fil21)
                $('#tcReqName').html(res.data.fil.tcReqName)
                $('#fil10').html(res.data.fil.fil10)
                $('#newazp').html(res.data.fil.xjgen02)
                $('#oldazp').html(res.data.fil.azp02)
                $('#oldazp').data('id',res.data.fil.filplant)
            }
        });

        $('.title-btn').on('click', '.add', function (e) {
            layer.open({
                type: 1,
                title: false,
                area: ['662px', '536px'],
                fixed: true,
                shadeClose: true,
                closeBtn: 0,
                content: $('#addDeviceTpl').html(),
                success: alertEvent.addDevice
            });
        })
        // 批量修改
        $(".editBatch").click(function () {
            var checkStatus = table.checkStatus('#LAY-app-content-comm');
            var arrs = [];
            for (var i = 0; i < checkStatus.data.length; i++) {
                arrs.push(checkStatus.data[i].tcDetailId);
            }
            if (checkStatus.data.length == 0) {
                parent.layer.msg('请至少选择一条数据', { icon: 2 });
                return;
            }
            sessionStorage.setItem('company',$('#oldazp').data('id'));
            admin.popup({
                title: '编辑设备信息',
                area: ['600px', '600px'],
                id: 'LAY-popup-content-tags',
                success: function (layero, index) {
                    view(this.id).render('transaction/branch-four/form2').done(function () {
                        //监听提交
                        form.on('submit(LAY-submit)', function (data) {
                            var field = data.field; //获取提交的字段
                            var data = {
                            }
                            data.devaltlist = arrs
                            data.tcDetailFia01 = field.fia01
                            data.tcDetailFia02 = field.fia02
                            data.tcDetailFia11 = field.tcDetailFia11.split("/")[1].replace(/\s+/g, "")
                            data.tcDetailFia10 = field.tcDetailFia10.split("/")[0].replace(/\s+/g, "")
                            data.tcDetailFia14 = field.tcDetailFia14
                                .split("/")[0].replace(/\s+/g, "")
                            data.tcDetailFia16 =
                                field
                                    .tcDetailFia16.split("/")[0]
                                    .replace(/\s+/g,
                                        "")
                            data.tcDetailFia15 =
                                field
                                    .tcDetailFia15.split("/")[0]
                                    .replace(/\s+/g,
                                        "")
                            data.tcDetailFia17 =
                                field
                                    .tcDetailFia17.split("/")[0]
                                    .replace(/\s+/g,
                                        "")
                            data.tcDetailFia011 =
                                field
                                    .tcDetailFia011.replace(/\s+/g, "")
                            data.tcDetailFiauser =
                                field
                                    .tcDetailFiauser.split("/")[0]
                                    .replace(
                                        /\s+/g, "")

                            var param = JSON.stringify(data)
                            admin.req({
                                url: api.host + api.updateByPrimaryBatch_llp,
                                dataType: 'json',

                                type: "POST",
                                contentType: 'application/json',
                                data: param,
                                done: function (res) {
                                    layui.table.reload(
                                        '#LAY-app-content-comm'
                                    ); //重载表格
                                    layer.closeAll()
                                }
                            });
                        });
                        // 取消关闭
                        $(".cancel").click(function () {
                            layer.close(index);
                        })
                    });
                }
            });
        })
        //批量删除
        $(".delBatch").click(function () {
            var checkStatus = table.checkStatus('#LAY-app-content-comm');
            var arrs = [];
            for (var i = 0; i < checkStatus.data.length; i++) {
                arrs.push(checkStatus.data[i].tcDetailId);
            }
            if (checkStatus.data.length == 0) {
                parent.layer.msg('请至少选择一条数据', { icon: 2 });
                return;
            }
            layer.confirm('确定要删除选中的设备吗？', {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        admin.req({
                            url: api.host + api.devdelDevAlt,
                            data: JSON.stringify(arrs),
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            done: function (res) {
                                layui.table.reload('#LAY-app-content-comm'); //重载表格
                                layer.closeAll()
                            }
                        });
                    }, function () {
                        layer.close();
                    });
        })
        var alertEvent = {
            addDevice: function (layero, index) {
                table.render({
                    elem: '#table-device-alert',
                    id: "#table-device-alert",
                    skin: 'nob',
                    height: 300,
                    loading: true,
                    method: 'POST',
                    toolbar: '',
                    headers: {
                        'token': layui.sessionData(setter.tableName).token
                    },
                    cols: [
                        [{
                            type: 'checkbox',
                            width: 20
                        },
                        {
                            field: 'fia01',
                            title: '设备编号'
                        },
                        {
                            field: 'fia02',
                            title: '设备名称'
                        },
                        {
                            field: 'fic02',
                            title: '设备类型'
                        },
                        {
                            field: 'pmc081',
                            title: '供应商'
                        },
                        {
                            field: 'tcSeriesName',
                            title: '设备系列'
                        },
                        {
                            field: 'fii02',
                            title: '设备型号'
                        }
                        ]
                    ],

                    url: api.host + api.FilFileDBSelectFiaFiles1,
                    request: {
                        pageName: 'pageNum',
                        limitName: 'pageSize',
                    },
                    page: {
                        layout: ['prev', 'page', 'next', 'skip'],
                        limits: [10, 20, 30, 50],
                        theme: '#1E9FFF'
                    },
                    parseData: function (res) { //res 即为原始返回的数据
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.message, //解析提示文本
                            "count": res.data.total, //解析数据长度
                            "data": res.data.list //解析数据列表
                        };
                    },
                    contentType: 'application/json',
                    where: {
                        fia01: "",
                        fia02: "",
                        fia09: "",
                        keyWord: "",
                        tcReqCoorig: $('#oldazp').data('id')
                    }
                });
                form.on('submit(queryTableProjectHeader)', function (data) {
                    var allData = data.field;
                    table.reload('#table-device-alert', {
                        where: allData,
                    });
                    return false;
                })
                form.on('submit(queryTableDevice)', function (data) {
                    var allData = data.field;
                    table.reload('#table-device-alert', {
                        where: allData,
                    });
                    return false;
                })
                utill.keyup('.search-blank-inner', '.search-blank-icon');
                $('body').off('click', '.footer').on('click', '.footer', function (e) {
                    var $target = $(e.target);
                    if ($target.hasClass('btn1')) {
                        var checkedArr = table.checkStatus('#table-device-alert').data;
                        layui.router().search.fil01
                        var arr = []
                        for (let index = 0; index < checkedArr.length; index++) {

                            arr.push({
                                tcDetailFia01: checkedArr[index].fia01,
                                tcDetailFil01: layui.router().search.fil01
                            })

                        }
                        admin.req({
                            url: api.host + api.devinsertDevAlt,
                            type: "post",
                            contentType: 'application/json',
                            data: JSON.stringify(arr),
                            done: function (res) {
                                rander()
                                layer.closeAll()
                            }
                        });
                        //接添加接口

                    } else if ($target.hasClass('btn2')) {
                        layer.closeAll()
                    }
                });
            }
        }



    });
</script>